package de.clapp.backend.requests;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import de.clapp.backend.database.Conn;
import de.clapp.backend.database.Queries;

public class LoginRequest implements ServerRequest
{
    /**
     * 
     */
    private static final long serialVersionUID = 96640482849904152L;
    private String login;
    private String pass;
    private int clubID = -1;
    
    public LoginRequest(String login,  String pass)
    {
        this.login = login.toUpperCase();
        this.pass = pass;
    }

    @Override
    public Type getReturnType()
    {
        return int.class;
    }

    @Override
    public Integer getItem()
    {
        return this.clubID;
    }

    @Override
    public void setItem(Object item)
    {
        this.clubID = (Integer) item;        
    }

    @Override
    public Object execute()
    {
        Connection con = Conn.getInstance().serverCon;
        int id = 0;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try
        {
            pstmt = con.prepareStatement(Queries.GET_LOGINID);
            pstmt.setString(1, this.pass);
            pstmt.setString(2, this.login);
            rs = pstmt.executeQuery();

            while (rs.next())
            {
               id = rs.getInt("ID_CLUB");
            }

            pstmt.close();
            rs.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(pstmt != null)
            {
                try
                {
                    pstmt.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        return id;
    }
    
}
